import 'package:find_friend/common/play_audio.dart';
import 'package:find_friend/const/const.dart';
import 'package:find_friend/pages/mine/mine_home.dart';
import 'package:find_friend/pages/xspace/model/school_model.dart';
import 'package:flutter/material.dart';

class AudioFlrWidget extends StatefulWidget {

  final Voice signvoice;
  final int duration;
  final String audiobackImgName;

  const AudioFlrWidget({Key key, this.signvoice, this.duration, this.audiobackImgName = "assets/yinpindi.png"}) : super(key: key);

  @override
  _AudioFlrWidgetState createState() => _AudioFlrWidgetState();
}

class _AudioFlrWidgetState extends State<AudioFlrWidget> {
  
  int voiceDuration = 0;
  bool isplay = false;

  @override
  initState() {
    super.initState();
    voiceDuration = widget.signvoice?.playtime ?? 0;
  }

  @override
  void didUpdateWidget(AudioFlrWidget oldWidget) {
    super.didUpdateWidget(oldWidget);
    // if(widget.signvoice.url != oldWidget.signvoice.url) {
    //   isplay = false;
    // }
    if(isplay == false) voiceDuration = widget.signvoice?.playtime ?? 0;
  }

  @override
  void dispose() {
    MyNetWorkPlayer.instance.stop();
    super.dispose();
  }
  
  _playVoice() {
    if(widget.signvoice == null) return;
    setState(() {
      isplay = !isplay;
    });
    MyNetWorkPlayer.instance.play(sender: widget.signvoice.url, audioduration: widget.signvoice?.playtime, onPlayDuration: (second){
      if(mounted){
        setState(() {
          voiceDuration = widget.signvoice.playtime - second == -1 ? widget.signvoice.playtime : widget.signvoice.playtime - second;
          if(widget.signvoice.playtime - second == -1) isplay = false;
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: _playVoice,
      child: MineFlrTestPlatWidget(
        width: 80.w,
        height: 35.w,
        audioImgBackName: widget.audiobackImgName,
        color: APP_main_color,
        padding: const EdgeInsets.symmetric(horizontal: 5),
        duration: voiceDuration == -1 ? (widget.signvoice?.playtime ?? 0) : voiceDuration,
        isplay: isplay,
      ),
    );
  }
}

